package com.example.italkapp2.controller.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.italkapp2.R;
import com.example.italkapp2.model.Model;
import com.example.italkapp2.model.bean.UserInfo;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EImageView;
import com.hyphenate.exceptions.HyphenateException;

//添加联系人页面
public class AddContactActivity extends Activity {
    private Button btn_add_find;
    private EditText et_add_name;
    private RelativeLayout rl_add;
    private ImageView iv_add_photo;
    private TextView tv_add_name;
    private Button btn_add_add;
    private Toolbar toolbar;

    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("text");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);

        initView();
        initListener();
    }

    private void initView(){
        btn_add_find=findViewById(R.id.btn_add_find);
        et_add_name=findViewById(R.id.et_add_name);
        rl_add=findViewById(R.id.rl_add);
        iv_add_photo=findViewById(R.id.iv_add_photo);
        tv_add_name=findViewById(R.id.tv_add_name);
        btn_add_add=findViewById(R.id.btn_add_add);
        toolbar=findViewById(R.id.toolbar);
    }

    private void initListener(){
        //查找按钮点击事件处理
        btn_add_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                find();
            }
        });

        //添加按钮点击事件处理
        btn_add_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });

        //返回按钮点击事件处理
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddContactActivity.this.finish();
            }
        });
    }

    private void find(){
        String searchName=et_add_name.getText().toString();

        //校验输入的名称
        if(searchName.isEmpty()){
            Toast.makeText(AddContactActivity.this,"输入的用户名不能为空",Toast.LENGTH_SHORT).show();
            return;

        }else{
            Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    //去服务器查找当前用户是否存在
                    userInfo = new UserInfo(searchName);

                    //更新UI显示
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            rl_add.setVisibility(View.VISIBLE);
                            tv_add_name.setText(userInfo.getName());
                        }
                    });
                }
            });
        }
    }

    private void add(){
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //去服务器添加好友
                try {
                    EMClient.getInstance().contactManager().addContact(userInfo.getName(),"添加好友");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddContactActivity.this,"发送添加好友邀请成功",Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddContactActivity.this,"发送添加好友邀请失败"+e.toString(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                //更新UI显示
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        rl_add.setVisibility(View.VISIBLE);
                        tv_add_name.setText(userInfo.getName());
                    }
                });
            }
        });
    }
}